function init(){
	
	var page = 0;
	var rows = 10;
	
	$('.state').on('click','li',function(){
		if($(this).html()=='订货单'){
			window.location.href = "/diconet/distributor/transaction_details";
		}else if($(this).html()=='退货单'){
			window.location.href = "/diconet/distributor/transaction_details_return";
		}
	});
	
	$(".select-box ul").on('click','li',function(){
	    $(this).parent().siblings('.show').val($(this).html());
	    $(this).parent().siblings('.hidden').val($(this).attr('data-id'));
	});
	
	$(".select-box div.fl").click(function(e){
	    e.stopPropagation();
	    var div=$(this);
	    div.children('ul').toggleClass('hide');
	    $(document).click(function() {
	        if (!div.children('ul').hasClass("hide")) {
	            div.children('ul').addClass("hide");
	        }
	    });
	});
	
	//w调用日期选择插件
	$('#datePicker').daterangepicker();
	
	//搜索商品
	$('.search-box span').click(function(){
	    var keyWord=$(this).siblings('input').val();
	    console.log(keyWord);
	    window.location.href = "/DistributorSystem/finance/findIncomeRecordList?condition="+keyWord;
	});
	
	$(function(){
	    $(".search-box input").focus(function(){
	        $(this).parent('div').css('border','1px solid #F5C63E');
	    }).blur(function(){
	        $(this).parent('div').css('border','1px solid #C0C0C0');
	    });
	});
	
	$(function(){
	    $(".state li").click(function(){
	        $(this).addClass('active').siblings('li').removeClass('active');
	    })
	});
	
	/**
	 * 确认收款的方法
	 */
	$('.product-list').on('click','.confirm',function(){
		var id = $(this).parent().attr('data-id');
		layer.confirm('是否确认收款？',{
			btn:['确定','取消']
		},function(index){
			// 点击确定按钮之后 执行确认收款操作
			$.ajax({
				type: "post",
				url: '/diconet/orderlang/pay/confirmReceivables',
				data: {
					id : id
				},
				dataType: 'json',
				success: function(data){
					if(data.success != 'T'){
						layer.msg(data.message,{icon: 2, time: 1000});
					}else{
						layer.msg(data.message,{icon: 1, time: 1000});
						setTimeout(function(){
							window.location.reload();
						},300);
					}
				},
				error: function(){
					layer.msg('确认收款失败',{icon: 2, time: 1000});
				}
			});
			layer.close(index);
		},function(){
			
		});
	});
	
	/**
	 * 查看的方法
	 */
	$('.product-list').on('click','.detail',function(){
		layer.msg($(this).parent().attr('data-id'),{icon: 1, time: 1000});
	});
	
	payStateMatching();
	loadComment(page, rows);
}

/**
 * 付款状态匹配
 */
function payStateMatching(){
    var pay = $('table .pay-state').children('p');
    $(pay).each(function(){
        var html=$(this).html();
        if(html=="未付款"){
            $(this).css('background','#4CB1FF');
        }else if(html=="已付款待审核"){
            $(this).css('background','#40C62E');
        }else if(html=="部分付款"){
            $(this).css('background','#40C62E');
        }else if(html=="已付款"){
            $(this).css('background','#40C62E');
        }
    });
    $('table tbody tr td.isConfirm').each(function(){
        var html=$(this).html();
        if(html=="已确认"){
            $(this).siblings('td').children('.confirm').remove();
        }
    });
}

/**
 * 获取所有的查询条件
 */
function findSearchConditions(){
	var strs = new Array();
	strs[0] = $('#productName').val();
	strs[1] = $('#warehouseId').val();
	strs[2] = $('#categoryId').val();
	var isRelease;
	var isReleases = $('#isRelease').children();
	isReleases.each(function(){
	    if($(this).hasClass('active')){
	    	isRelease = $(this).attr('data-id');
	    	return false;
	    }
	});
	strs[3] = isRelease;
	return strs;
}

/**
 * 加载列表数据
 * @param page
 * @param rows
 */
function loadComment(page, rows) {
	$.ajax({
		type: "post",
		url: '/diconet/orderlang/pay/findTransactionDetailsList',
		data: {
			page : page,
			rows : rows
		},
		dataType: 'json',
		success: function(data){
			var commentContent = '';
			if(data) {
				for(var i=0; i<data.rows.length; i++) {
					var comment = data.rows[i];
					var date1=comment.publishDate == null?'':comment.publishDate;
					commentContent += '<tr>';
					commentContent += '<td>'+comment.name+'';// 客户
					commentContent += '<td>'+comment.orderCode+'</td>';// 订单号
					var paymentState;
					comment.paymentState == 1 ? paymentState='已结清' : paymentState = '付款中';
					commentContent += '<td>'+paymentState+'</td>';// 付款状态
					commentContent += '<td>'+comment.orderAmount+'</td>';// 订单金额
					commentContent += '<td>'+comment.paymentTransaction+'</td>';// 支付流水号
					commentContent += '<td>'+comment.payAmount+'</td>';// 支付金额
					commentContent += '<td>'+comment.payType+'</td>';// 支付方式
					var isConfirm;
					comment.isConfirm == 1 ? isConfirm='已确认' : isConfirm = '未确认';
					commentContent += '<td>'+isConfirm+'</td>';// 款项确认
					var confirmTime;
					comment.confirmTime == null ? confirmTime = '' : confirmTime = comment.confirmTime;
					commentContent += '<td>'+confirmTime+'</td>';// 时间
					if(comment.isConfirm != 1){
						commentContent += '<td data-id="'+comment.id+'"><button class="detail">查看</button><button class="confirm">确认收款</button></td>';
					}else{
						commentContent += '<td data-id="'+comment.id+'"><button class="detail">查看</button></td>';
					}
					commentContent += '</tr>';
				}
				$('.product-list tbody').html(commentContent);
				var dataPage = data.page;
				var pageContent="";
				if(dataPage.firstPage) {
					pageContent += '<span class="disabled">首页</span>';
					pageContent += '<span class="disabled">上一页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.first+','+rows+',\''+productName+'\',\''+ warehouseId+'\',\''+ categoryId+'\',\''+isRelease+'\');">首页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.prev+','+rows+',\''+productName+'\',\''+ warehouseId+'\',\''+ categoryId+'\',\''+isRelease+'\');">上一页</a>';
				}
				var maxPage = dataPage.page+2 < dataPage.last?dataPage.page+2:dataPage.last;
				if(dataPage.page >= 4 && dataPage.page + 2 < dataPage.last){
					for(var i=dataPage.page-2; i<= maxPage; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+productName+'\',\''+ warehouseId+'\',\''+ categoryId+'\',\''+isRelease+'\');">'+i+'</a>';
						}
					}
				}else if(dataPage.page > 4 && dataPage.page + 2 >= dataPage.last){
					for(var i=dataPage.last-4; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+productName+'\',\''+ warehouseId+'\',\''+ categoryId+'\',\''+isRelease+'\');">'+i+'</a>';
						}
					}
				}else if(dataPage.last < 5){
					for(var i=dataPage.first; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+productName+'\',\''+ warehouseId+'\',\''+ categoryId+'\',\''+isRelease+'\');">'+i+'</a>';
						}
					}
				}else{
					for(var i=dataPage.first; i<=5; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+productName+'\',\''+ warehouseId+'\',\''+ categoryId+'\',\''+isRelease+'\');">'+i+'</a>';
						}
					}
				}
				if(dataPage.lastPage) {
					pageContent += '<span class="disabled">下一页</span>';
					pageContent += '<span class="disabled">尾页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.next+','+rows+',\''+productName+'\',\''+ warehouseId+'\',\''+ categoryId+'\',\''+isRelease+'\');">下一页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.last+','+rows+',\''+productName+'\',\''+ warehouseId+'\',\''+ categoryId+'\',\''+isRelease+'\');">尾页</a>';
				}
			}
			$(".quotes").html(pageContent);
		},
		error : function(data){
			layer.msg('请求失败', {time:500,icon:5}); 
		}
	});
}

init();
